package com.xiaohe66.web.infrastructure.mybatis.im.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaohe66.web.domain.account.value.AccountId;
import com.xiaohe66.web.domain.im.agg.ImUser;
import com.xiaohe66.web.domain.im.repository.ImUserRepository;
import com.xiaohe66.web.domain.im.value.ImUserId;
import com.xiaohe66.web.infrastructure.mybatis.im.convert.ImUserDoConverter;
import com.xiaohe66.web.infrastructure.mybatis.im.mapper.ImUserMapper;
import com.xiaohe66.web.infrastructure.mybatis.im.model.ImUserDo;
import com.xiaohe66.web.integration.AbstractMybatisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Comparator;
import java.util.List;

/**
 * @author xiaohe
 * @since 2023-09-07 16:22
 */
@Repository
@Slf4j
@RequiredArgsConstructor
public class ImUserRepositoryImpl
        extends AbstractMybatisService<ImUserDoConverter, ImUserMapper, ImUserDo, ImUser, ImUserId>
        implements ImUserRepository {

    @Override
    public ImUser getByCreateId(AccountId createId) {

        ImUserDo query = new ImUserDo();
        query.setCreateId(createId.getValue());

        ImUserDo imUserDo = getOne(new QueryWrapper<>(query));

        return dataConverter.toAgg(imUserDo);
    }

    @Override
    public List<ImUser> listAll() {

        List<ImUserDo> list = list();

        list.sort(Comparator.comparing(ImUserDo::getNickname));

        return dataConverter.toAgg(list);
    }
}
